/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj80;
using EnvDTE;
using DaveSexton.DocProject.Wizards.Foundation;

namespace DaveSexton.DocProject.Wizards
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
	public sealed class ChooseSourcesStep : WizardStep
	{
		#region Public Properties
		public override string Name
		{
			get
			{
				return WizardText.ChooseSourcesStepName;
			}
		}

		public override bool CanPreloadUserInterface
		{
			get
			{
				return true;
			}
		}

		public IEnumerable<IAnyProject> SelectedProjects
		{
			get
			{
				return new MarshalByRefEnumerable<IAnyProject>(Control.SelectedProjects);
			}
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
		public SourceCollection ExternalSources
		{
			get
			{
				return Control.ExternalSources;
			}
			set
			{
				Control.ExternalSources = value;
			}
		}

		public override bool Visible
		{
			get
			{
				return SourceProjects.Count > 0;
			}
		}
		#endregion

		#region Private / Protected
		private ChooseSourcesControl Control
		{
			get
			{
				if (control == null)
				{
					control = new ChooseSourcesControl(docProject);
					control.AddProjectRange(SourceProjects);
				}

				return control;
			}
		}

		/// <summary>
		/// Gets a list of potential source projects.
		/// </summary>
		private IList<IAnyProject> SourceProjects
		{
			get
			{
				if (sourceProjects == null)
					sourceProjects = new List<IAnyProject>(DocProjectEnvironment.FindProjects(delegate(IAnyProject currentProject)
					{
						return currentProject.Project != docProject.Project && ProjectFactory.IsSourceProject(currentProject);
					}));

				return sourceProjects;
			}
		}

		private ChooseSourcesControl control;
		private IList<IAnyProject> sourceProjects;
		private readonly IDocProject docProject;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ChooseSourcesStep" /> class.
		/// </summary>
		public ChooseSourcesStep(IDocProject docProject)
		{
			if (docProject == null)
				throw new ArgumentNullException("docProject");

			this.docProject = docProject;
		}
		#endregion

		#region Methods
		protected override object GetUserInterface(WizardController controller, bool preloading)
		{
			return Control;
		}

		protected override void WizardFinished(WizardController controller)
		{
			if (Visible)
			{
				foreach (IAnyProject project in SelectedProjects)
					docProject.AddReference(project.Project);

				DocProjectOptions.AddExternalSources(docProject, ExternalSources);
			}
		}

		protected override void WizardCanceled(WizardController controller)
		{
			// do nothing
		}
		#endregion
	}
}
